﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using LockCrypt.Core;
using LockCrypt.DAL;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A form for editing account types.
    /// </summary>
    public partial class FrmEditType : Form {
        private bool editing = false,
                     cancelDialog = false;
        private List<ITemplate> templates;
        public ITemplate SelectedTemplate { get; set; }

        /// <summary>
        /// Initializes a new form to add a new type.
        /// </summary>
        /// <param name="templates">The templates to edit.</param>
        public FrmEditType(List<ITemplate> templates) {
            InitializeComponent();
            this.templates = templates;
            //this.types = DalConfigurationManager.Provider.GetTemplates();
            this.Text = I18nUtils.GetString("Strings", "NewTemplate");
            itmCancel.Click += (s, e) => {
                cancelDialog = true;
                this.DialogResult = DialogResult.Cancel;
            };
            itmSave.Click += (s, e) => {
                if(ValidateType()) {
                    this.DialogResult = DialogResult.OK;
                }
            };
            this.Closing += delegate(object sender, CancelEventArgs e) {
                if(!cancelDialog) {
                    if(!ValidateType()) {
                        e.Cancel = true;
                    } else {
                        SelectedTemplate.Name = txtName.Text;
                        SelectedTemplate.ShowNotes = chkShowNotes.Checked;
                        char[] splitters = Environment.NewLine.ToCharArray().Concat(new[] { ',' }).ToArray();
                        string[] fieldNames = txtFields.Text.Split(splitters);
                        SelectedTemplate.Fields.Clear();
                        //foreach(string fieldName in fieldNames) {
                        for(int i=0;i<fieldNames.Length;i++) {
                            if(fieldNames[i].Trim().Length == 0)
                                continue;
                            SelectedTemplate.Fields.Add(new FieldBase() {
                                Name = fieldNames[i],
                                SortIndex = i
                            });
                        }
                        this.DialogResult = DialogResult.OK;
                    }
                }
            };
            SelectedTemplate = new TemplateBase();
        }

        /// <summary>
        /// Initializes a new form to edit an existing type.
        /// </summary>
        /// <param name="templates">The templates.</param>
        /// <param name="templateToEdit">The template to edit.</param>
        public FrmEditType(List<ITemplate> templates, ITemplate templateToEdit)
            : this(templates) {
            editing = true;
            this.SelectedTemplate = templateToEdit;
            txtName.Text = templateToEdit.Name;
            //txtFields.Text = Utils.Implode(typeToEdit.Fields, Environment.NewLine);
            txtFields.Text = templateToEdit.Fields.Aggregate("", (items, item) => items + item.Name + Environment.NewLine);// Utils.Implode(typeToEdit.Fields, Environment.NewLine);
            chkShowNotes.Checked = templateToEdit.ShowNotes;
        }

        /// <summary>
        /// Validates that the type name is not blank and that field names have been entered.
        /// </summary>
        /// <returns><c>true</c> if the type is valid, otherwise <c>false</c></returns>
        private bool ValidateType() {
            if(string.IsNullOrEmpty(txtName.Text)) {
                MessageBox.Show(I18nUtils.GetString("Errors", "TemplateNameRequired"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            } else if(!editing) {
                foreach(ITemplate type in templates) {
                    if(type.Name == txtName.Text) {
                        MessageBox.Show(I18nUtils.GetString("Errors", "TemplateNameExists"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                        txtName.SelectAll();
                        txtName.Focus();
                        return false;
                    }
                }
            }
            return true;
        }
    }
}